package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Employee;
import com.model.User;
import com.service.EmpService;
import com.util.Conversion;
import com.util.JdbcUtil;

/**
 * Servlet implementation class empservlet
 */
@WebServlet("/emp")
public class empservlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
   
	EmpService empService =new EmpService();
	
	protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		JdbcUtil jdbc = new JdbcUtil();
		String sql ="select * from employee order by eid";
		List<Employee> emps = jdbc.queryPreparedStatement(sql, Employee.class);
			
		request.setAttribute("emps",emps);
		//System.out.println(emps.isEmpty());
		request.getRequestDispatcher("emplyee_list.jsp").forward(request, response);
		
		
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		String eid = request.getParameter("eid");
		JdbcUtil jdbc = new JdbcUtil();
		String sql="delete from employee where eid=?";
		System.out.println("eid");
		try {
			jdbc.updatePreparedStatement(sql,eid);
			request.getRequestDispatcher("emp?action=query").forward(request, response);
		
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "删除失败，请稍后再试");
		}
	}
protected void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		
		String eid = request.getParameter("eid");
		String ename = request.getParameter("ename");
		String epassword = request.getParameter("epassword");
		String ephone = request.getParameter("ephone");
		String eaddress = request.getParameter("eaddress");
		String wid = request.getParameter("wid");
		System.out.println(eid+","+ename+","+wid);
		User user = new User();
		Conversion.convert(user, request);
		JdbcUtil jdbc = new JdbcUtil();
		String sql="insert into employee values (null, ?, ?, ?, ?, ?)";
		
		try {
			//更新不需要指定类Employee.class
			jdbc.updatePreparedStatement(sql,ename,epassword,ephone,eaddress,Integer.parseInt(wid));
	/*jdbc.updatePreparedStatement(sql,Employee.class,ename,epassword,ephone,eaddress,1001);*/
			System.out.println("插入成功");
			request.getRequestDispatcher("emp?action=query").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "新增失败，请检查录入员工号是否重复！！");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void update(HttpServletRequest request, HttpServletResponse response){
		String name = request.getParameter("ename");
		String eid = request.getParameter("eid");
		
		System.out.println(name);
		request.setAttribute("name", name);
		request.setAttribute("eid", eid);
		
		try {
			request.getRequestDispatcher("emplyee_password.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void alert(HttpServletRequest request, HttpServletResponse response){
		try {
			String pwd = request.getParameter("newpass");
			Integer eid = Integer.parseInt(request.getParameter("eid"));
			empService.alert(eid, pwd);
			query(request,response);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
}
